package com.web.bigdata;

import com.web.bigdata.pojo.Article;
import com.web.bigdata.pojo.ArticleTopnView;
import com.web.bigdata.pojo.BigdataArticleTopn;
import com.web.bigdata.repostory.ArticleRepostory;
import com.web.bigdata.repostory.ArticleTopnRepostory;
import com.web.bigdata.response.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/topn")
public class Controller {

    @Autowired
    private ArticleTopnRepostory topnRepostory;
    @Autowired
    private ArticleRepostory articleRepostory;
//    @Autowired
//    private ArticleTopNRepository topNRepostory;

    @GetMapping("/{year}/{month}/{day}")
    public ServerResponse getArticleTopN(@PathVariable String year,
            @PathVariable String month, @PathVariable String day){


        String time;
        time = year +"-"+ month +"-"+ day;
        System.out.println(time);
        BigdataArticleTopn articleTopn = new BigdataArticleTopn();
        articleTopn.setTime(time);
        Example<BigdataArticleTopn> example = Example.of(articleTopn);
        final List<BigdataArticleTopn> list = topnRepostory.findAll(example);
        List<ArticleTopnView> views = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            final BigdataArticleTopn topn = list.get(i);
            System.out.println(topn.getArticleId());
            final String title = articleRepostory.findById(topn.getArticleId()).get().getTitle();
            System.out.println(title);
            ArticleTopnView view = new ArticleTopnView(topn.getId(),title,topn.getWeight(),topn.getTime());
            System.out.println(view);
            views.add(view);
        }
//        list.forEach(System.out::println);
        return ServerResponse.createBySuccess(views);
    }
}
